﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace Ultilities
{
    public class frmShowMessageDialogEx : Form
    {
        // Fields
        private string _version = "";
        private SimpleButton btnOne;
        private double captionLength;
        private IContainer components;
        private const int FORM_HEADER_HEIGHT = 30;
        private double formWidth;
        private LabelControl lblBuildVersion;
        private LabelControl lblPrompt;
        private const int LINE_SPACING = 1;
        private LinkLabel linkEmailToLV;
        private int maxWidth;
        private string mCaption;
        private MessageBoxButtons msgButtons;
        private MessageBoxDefaultButton msgDefaultButton;
        private PictureBox msgIcon;
        private MessageBoxIcon msgIcons;
        private string mText;
        //private OfficeFormAdorner officeFormAdorner1;
        private PanelControl pnlBottom;
        private PanelControl pnlHandle;
        private PanelControl pnlIcon;
        private PanelControl pnlMain;
        private PanelControl pnlRight;
        private PanelControl pnlTop;
        private double promptLength;
        private double rowCount;
        private string[] strMsgArr;

        // Methods
        public frmShowMessageDialogEx()
        {
            this.InitializeComponent();
            //this.linkEmailToLV.LinkClicked += new LinkLabelLinkClickedEventHandler(this.linkEmailToLV_LinkClicked);
            if (this._version == "")
            {
                this.GetVersion();
            }
            this.lblBuildVersion.Text = this._version;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private int getLengthText(string strText, ref string strMaxLengthLine)
        {
            int length = 0;
            int index = 0;
            this.strMsgArr = strText.Split(Environment.NewLine.ToCharArray());
            strMaxLengthLine = this.strMsgArr[0];
            length = strMaxLengthLine.Length;
            for (index = 1; index < (this.strMsgArr.Length - 1); index++)
            {
                if (length < this.strMsgArr[index].Length)
                {
                    strMaxLengthLine = this.strMsgArr[index];
                    length = strMaxLengthLine.Length;
                }
            }
            return length;
        }

        private void GetVersion()
        {
            Assembly assembly = null;
            AssemblyName name = null;
            try
            {
                assembly = Assembly.LoadFrom(Application.ExecutablePath);
                if (assembly != null)
                {
                    name = assembly.GetName();
                }
                if (name != null)
                {
                    this._version = "Built: " + name.Version.ToString();
                }
            }
            catch
            {
            }
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
            this.components = new Container();
            this.pnlHandle = new PanelControl();
            pnlHandle.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.linkEmailToLV = new LinkLabel();
            this.btnOne = new SimpleButton();

            this.pnlIcon = new PanelControl();
            pnlIcon.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.msgIcon = new PictureBox();

            this.pnlMain = new PanelControl();
            pnlMain.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.lblPrompt = new LabelControl();

            this.pnlRight = new PanelControl();
            pnlRight.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;

            this.pnlBottom = new PanelControl();
            pnlBottom.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;

            this.pnlTop = new PanelControl();
            pnlTop.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            //this.officeFormAdorner1 = new OfficeFormAdorner(this.components);
            this.lblBuildVersion = new LabelControl();
            this.pnlHandle.SuspendLayout();
            this.pnlIcon.SuspendLayout();
            ((ISupportInitialize)this.msgIcon).BeginInit();
            this.pnlMain.SuspendLayout();
            //((ISupportInitialize)this.officeFormAdorner1).BeginInit();
            base.SuspendLayout();
            this.pnlHandle.Controls.Add(this.linkEmailToLV);
            this.pnlHandle.Controls.Add(this.btnOne);
            this.pnlHandle.Controls.Add(this.lblBuildVersion);
            this.pnlHandle.Dock = DockStyle.Bottom;
            this.pnlHandle.Location = new Point(0, 0x58);
            this.pnlHandle.Name = "pnlHandle";
            this.pnlHandle.Size = new Size(0x15d, 0x25);
            this.pnlHandle.TabIndex = 0;
            this.linkEmailToLV.AutoSize = true;
            this.linkEmailToLV.Location = new Point(-1, 9);
            this.linkEmailToLV.Name = "linkEmailToLV";
            this.linkEmailToLV.Size = new Size(0x30, 13);
            this.linkEmailToLV.TabIndex = 7;
            this.linkEmailToLV.TabStop = true;
            this.linkEmailToLV.Text = "Send To";
            this.btnOne.Anchor = AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Top;
            this.btnOne.Font = new Font("Tahoma", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.btnOne.Location = new Point(0x10c, 6);
            this.btnOne.Name = "btnOne";
            //this.btnOne.ShowFocusRectangle = false;
            this.btnOne.Size = new Size(0x45, 0x18);
            this.btnOne.TabIndex = 0;
            this.btnOne.Text = "Đồng \x00fd";
            //this.btnOne.UseCompatibleTextRendering = true;
            //this.btnOne.VisualStyle = VisualStyle.Office2010;
            //this.pnlIcon.BackColor = Color.Transparent;
            this.pnlIcon.Controls.Add(this.msgIcon);
            this.pnlIcon.Dock = DockStyle.Left;
            this.pnlIcon.Location = new Point(0, 0);
            this.pnlIcon.Name = "pnlIcon";
            this.pnlIcon.Size = new Size(0x38, 0x58);
            this.pnlIcon.TabIndex = 10;
            this.msgIcon.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.msgIcon.Location = new Point(12, 9);
            this.msgIcon.Name = "msgIcon";
            this.msgIcon.Size = new Size(0x20, 0x40);
            this.msgIcon.SizeMode = PictureBoxSizeMode.CenterImage;
            this.msgIcon.TabIndex = 4;
            this.msgIcon.TabStop = false;
            this.pnlMain.Controls.Add(this.lblPrompt);
            this.pnlMain.Controls.Add(this.pnlRight);
            this.pnlMain.Controls.Add(this.pnlBottom);
            this.pnlMain.Controls.Add(this.pnlTop);
            this.pnlMain.Dock = DockStyle.Fill;
            this.pnlMain.Location = new Point(0x38, 0);
            this.pnlMain.Name = "pnlMain";
            this.pnlMain.Size = new Size(0x125, 0x58);
            this.pnlMain.TabIndex = 11;
            //this.lblPrompt.BackColor = Color.Transparent;
            this.lblPrompt.Dock = DockStyle.Fill;
            this.lblPrompt.Font = new Font("Tahoma", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.lblPrompt.Location = new Point(0, 12);
            this.lblPrompt.Name = "lblPrompt";
            this.lblPrompt.Size = new Size(0x125, 0x40);
            this.lblPrompt.TabIndex = 6;
            this.lblPrompt.Text = "Cộng h\x00f2a x\x00e3 hội chủ nghĩa Việt Nam";
            //this.lblPrompt.TextAlign = ContentAlignment.MiddleLeft;
            this.pnlRight.Dock = DockStyle.Right;
            this.pnlRight.Location = new Point(0x125, 12);
            this.pnlRight.Name = "pnlRight";
            this.pnlRight.Size = new Size(0, 0x40);
            this.pnlRight.TabIndex = 5;
            //this.pnlBottom.BackColor = Color.Transparent;
            this.pnlBottom.Dock = DockStyle.Bottom;
            this.pnlBottom.Location = new Point(0, 0x4c);
            this.pnlBottom.Name = "pnlBottom";
            this.pnlBottom.Size = new Size(0x125, 12);
            this.pnlBottom.TabIndex = 4;
            //this.pnlTop.BackColor = Color.Transparent;
            this.pnlTop.Dock = DockStyle.Top;
            this.pnlTop.Location = new Point(0, 0);
            this.pnlTop.Name = "pnlTop";
            this.pnlTop.Size = new Size(0x125, 12);
            this.pnlTop.TabIndex = 3;
            //this.officeFormAdorner1.Form = this;
            this.lblBuildVersion.Dock = DockStyle.Bottom;
            this.lblBuildVersion.Font = new Font("Microsoft Sans Serif", 6.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.lblBuildVersion.Location = new Point(0, 0);
            this.lblBuildVersion.Name = "lblBuildVersion";
            this.lblBuildVersion.Size = new Size(0xa5, 0x25);
            this.lblBuildVersion.TabIndex = 8;
            this.lblBuildVersion.Text = "Build: 1.0.0.0";
            //this.lblBuildVersion.TextAlign = ContentAlignment.BottomLeft;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.FromArgb(0xc4, 0xdd, 0xff);
            base.ClientSize = new Size(0x15d, 0x7d);
            base.ControlBox = false;
            base.Controls.Add(this.pnlMain);
            base.Controls.Add(this.pnlIcon);
            base.Controls.Add(this.pnlHandle);
            base.FormBorderStyle = FormBorderStyle.FixedDialog;
            base.MaximizeBox = false;
            this.MaximumSize = new Size(450, 400);
            base.MinimizeBox = false;
            this.MinimumSize = new Size(0x143, 0x8f);
            base.Name = "frmShowMessageDialogEx";
            base.ShowIcon = false;
            base.ShowInTaskbar = false;
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "Show Message";
            this.pnlHandle.ResumeLayout(false);
            this.pnlHandle.PerformLayout();
            this.pnlIcon.ResumeLayout(false);
            ((ISupportInitialize)this.msgIcon).EndInit();
            this.pnlMain.ResumeLayout(false);
            //((ISupportInitialize)this.officeFormAdorner1).EndInit();
            base.ResumeLayout(false);
        }

        //private void linkEmailToLV_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        //{
        //    string cipherString = ConfigurationSettings.AppSettings["14"];
        //    if (cipherString == null)
        //    {
        //        cipherString = "";
        //    }
        //    if (cipherString != "")
        //    {
        //        cipherString = CryptorEngine.Decrypt(cipherString, true);
        //    }
        //    string strSubject = "System error : " + DateTime.Now.ToString();
        //    string str3 = DateTime.Today.ToString("yyyyMM");
        //    string strAttachmentFileName = Application.StartupPath + @"\Logs\Errors_" + str3 + ".txt";
        //    string strBody = Environment.NewLine + "Refer attachments, pls!!!" + Environment.NewLine;
        //    if (this._version != "")
        //    {
        //        strBody = this._version + Environment.NewLine;
        //    }
        //    MAPI mapi = new MAPI();
        //    mapi.AddAttachment(strAttachmentFileName);
        //    mapi.AddRecipientTo(cipherString);
        //    mapi.SendMailPopup(strSubject, strBody);
        //}

        private void SetButtonText()
        {
            this.linkEmailToLV.Text = ErpMultiLanguage.GetString("COMMON", "MAILTO");
            switch (this.msgButtons)
            {
                case MessageBoxButtons.OK:
                    this.btnOne.Text = ErpMultiLanguage.GetString("COMMON", "OK");
                    this.btnOne.DialogResult = DialogResult.OK;
                    base.AcceptButton = this.btnOne;
                    return;

                case MessageBoxButtons.OKCancel:
                    this.btnOne.Text = ErpMultiLanguage.GetString("COMMON", "OK");
                    this.btnOne.DialogResult = DialogResult.OK;
                    base.AcceptButton = this.btnOne;
                    base.CancelButton = this.btnOne;
                    return;

                case MessageBoxButtons.AbortRetryIgnore:
                    this.btnOne.Text = ErpMultiLanguage.GetString("COMMON", "YES");
                    this.btnOne.DialogResult = DialogResult.Yes;
                    base.AcceptButton = this.btnOne;
                    return;

                case MessageBoxButtons.YesNoCancel:
                    this.btnOne.Text = ErpMultiLanguage.GetString("COMMON", "YES");
                    this.btnOne.DialogResult = DialogResult.Yes;
                    base.AcceptButton = this.btnOne;
                    return;

                case MessageBoxButtons.YesNo:
                    this.btnOne.Text = ErpMultiLanguage.GetString("COMMON", "YES");
                    this.btnOne.DialogResult = DialogResult.Yes;
                    base.AcceptButton = this.btnOne;
                    base.AcceptButton = this.btnOne;
                    return;

                case MessageBoxButtons.RetryCancel:
                    this.btnOne.Text = ErpMultiLanguage.GetString("COMMON", "RETRY");
                    this.btnOne.DialogResult = DialogResult.Retry;
                    base.AcceptButton = this.btnOne;
                    base.CancelButton = this.btnOne;
                    return;
            }
        }

        private void SetDefaultButton()
        {
            switch (this.msgButtons)
            {
                case MessageBoxButtons.OK:
                    this.SetTabIndex(1, this.msgDefaultButton);
                    return;

                case MessageBoxButtons.OKCancel:
                case MessageBoxButtons.YesNo:
                case MessageBoxButtons.RetryCancel:
                    this.SetTabIndex(2, this.msgDefaultButton);
                    return;

                case MessageBoxButtons.AbortRetryIgnore:
                    this.SetTabIndex(3, this.msgDefaultButton);
                    break;

                case MessageBoxButtons.YesNoCancel:
                    break;

                default:
                    return;
            }
        }

        private void SetMessIcon()
        {
            switch (this.msgIcons)
            {
                case MessageBoxIcon.Question:
                    if (this.msgIcon.Image != null)
                    {
                        this.msgIcon.Image.Dispose();
                    }
                    this.msgIcon.Image = IconImages.QuestionIcon;
                    break;

                case MessageBoxIcon.Exclamation:
                    if (this.msgIcon.Image != null)
                    {
                        this.msgIcon.Image.Dispose();
                    }
                    this.msgIcon.Image = IconImages.WarningIcon;
                    break;

                case MessageBoxIcon.Asterisk:
                    if (this.msgIcon.Image != null)
                    {
                        this.msgIcon.Image.Dispose();
                    }
                    this.msgIcon.Image = IconImages.InfoIcon;
                    break;

                case MessageBoxIcon.None:
                    if (this.msgIcon.Image != null)
                    {
                        this.msgIcon.Image.Dispose();
                    }
                    this.msgIcon.Image = null;
                    break;

                case MessageBoxIcon.Hand:
                    if (this.msgIcon.Image != null)
                    {
                        this.msgIcon.Image.Dispose();
                    }
                    this.msgIcon.Image = IconImages.ErrorIcon;
                    break;
            }
            if (this.Text == string.Empty)
            {
                this.msgIcon.Top += 15;
            }
        }

        private void SetTabIndex(int buttonCount, MessageBoxDefaultButton msgDefaultButton)
        {
            switch (buttonCount)
            {
                case 1:
                    this.btnOne.TabIndex = 0;
                    return;

                case 2:
                    if (msgDefaultButton != MessageBoxDefaultButton.Button1)
                    {
                        this.btnOne.TabIndex = 0;
                        return;
                    }
                    this.btnOne.TabIndex = 0;
                    return;

                case 3:
                    switch (msgDefaultButton)
                    {
                        case MessageBoxDefaultButton.Button1:
                            this.btnOne.TabIndex = 0;
                            return;

                        case MessageBoxDefaultButton.Button2:
                            this.btnOne.TabIndex = 0;
                            return;

                        case MessageBoxDefaultButton.Button3:
                            this.btnOne.TabIndex = 0;
                            return;
                    }
                    return;
            }
        }

        public DialogResult ShowDialog(string text, string caption, MessageBoxButtons msgButtons, MessageBoxIcon msgIcons, MessageBoxDefaultButton msgDefaultButton)
        {
            text = text.Replace(@"\n", Environment.NewLine);
            double num = 0.0;
            this.mText = text;
            this.mCaption = caption;
            this.msgButtons = msgButtons;
            this.msgIcons = msgIcons;
            this.msgDefaultButton = msgDefaultButton;
            string strMaxLengthLine = "";
            this.promptLength = this.getLengthText(text, ref strMaxLengthLine);
            this.maxWidth = (this.MaximumSize.Width - this.pnlIcon.Width) - this.pnlRight.Width;
            this.captionLength = caption.Length;
            this.formWidth = ((this.pnlIcon.Width + (((double)(((int)this.promptLength) * this.lblPrompt.Font.Size)) / 1.77)) + this.pnlRight.Width) + 51.0;
            num = ((double)(((int)this.captionLength) * this.lblPrompt.Font.Size)) / 1.48;
            for (int i = 0; i < this.strMsgArr.Length; i++)
            {
                if ((this.strMsgArr[i] != string.Empty) && (this.strMsgArr[i] != Environment.NewLine))
                {
                    double num3 = 0.0;
                    num3 = ((double)(this.strMsgArr[i].Length * this.lblPrompt.Font.Size)) / 1.77;
                    if (num3 > this.maxWidth)
                    {
                        if ((num3 % ((double)this.maxWidth)) == 0.0)
                        {
                            this.rowCount++;
                        }
                        else
                        {
                            this.rowCount += ((((double)(this.strMsgArr[i].Length * this.lblPrompt.Font.Size)) / 1.77) / ((double)this.maxWidth)) + 1.0;
                        }
                    }
                    else
                    {
                        this.rowCount++;
                    }
                }
            }
            if (num > this.formWidth)
            {
                this.formWidth = num;
            }
            Form activeForm = Form.ActiveForm;
            this.lblPrompt.Text = this.mText;
            this.Text = this.mCaption;
            this.SetButtonText();
            this.SetMessIcon();
            this.SetDefaultButton();
            Size size = TextRenderer.MeasureText(strMaxLengthLine, this.lblPrompt.Font);
            base.Width = (int)this.formWidth;
            base.Height = (((30 + this.pnlTop.Height) + this.pnlBottom.Height) + this.pnlHandle.Height) + ((int)(this.rowCount * (size.Height + 1)));
            base.StartPosition = FormStartPosition.CenterScreen;
            return base.ShowDialog(activeForm);
        }

        public DialogResult ShowDialog(IWin32Window owner, string text, string caption, MessageBoxButtons msgButtons, MessageBoxIcon msgIcons, MessageBoxDefaultButton msgDefaultButton)
        {
            text = text.Replace(@"\n", Environment.NewLine);
            double num = 0.0;
            this.mText = text;
            this.mCaption = caption;
            this.msgButtons = msgButtons;
            this.msgIcons = msgIcons;
            this.msgDefaultButton = msgDefaultButton;
            string strMaxLengthLine = "";
            this.promptLength = this.getLengthText(text, ref strMaxLengthLine);
            this.maxWidth = ((this.MaximumSize.Width - this.pnlIcon.Width) - this.pnlRight.Width) - 15;
            this.captionLength = caption.Length;
            this.formWidth = ((this.pnlIcon.Width + (((double)(((int)this.promptLength) * this.lblPrompt.Font.Size)) / 1.77)) + this.pnlRight.Width) + 51.0;
            num = ((double)(((int)this.captionLength) * this.lblPrompt.Font.Size)) / 1.48;
            for (int i = 0; i < this.strMsgArr.Length; i++)
            {
                if ((this.strMsgArr[i] != string.Empty) && (this.strMsgArr[i] != Environment.NewLine))
                {
                    double num3 = 0.0;
                    num3 = ((double)(this.strMsgArr[i].Length * this.lblPrompt.Font.Size)) / 1.77;
                    if (num3 > this.maxWidth)
                    {
                        if ((num3 % ((double)this.maxWidth)) == 0.0)
                        {
                            this.rowCount++;
                        }
                        else
                        {
                            this.rowCount += ((((double)(this.strMsgArr[i].Length * this.lblPrompt.Font.Size)) / 1.77) / ((double)this.maxWidth)) + 1.0;
                        }
                    }
                    else
                    {
                        this.rowCount++;
                    }
                }
            }
            if (num > this.formWidth)
            {
                this.formWidth = num;
            }
            this.lblPrompt.Text = this.mText;
            this.Text = this.mCaption;
            this.SetButtonText();
            this.SetMessIcon();
            this.SetDefaultButton();
            Size size = TextRenderer.MeasureText(strMaxLengthLine, this.lblPrompt.Font);
            base.Width = (int)this.formWidth;
            base.Height = (((30 + this.pnlTop.Height) + this.pnlBottom.Height) + this.pnlHandle.Height) + ((int)(this.rowCount * (size.Height + 1)));
            base.StartPosition = FormStartPosition.CenterParent;
            return base.ShowDialog(owner);
        }
    }

 

}
